package com.blog.controller.backend;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.blog.entity.Product;
import com.blog.myUtils.UploadHelper;
import com.blog.service.ProductService;
import com.blog.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mars123
 * @since 2019-07-02
 */
@Controller("backendProductController")
@RequestMapping("/backend/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTypeService productTypeService;

    @RequestMapping("/index")
    public String index(Model model, Integer curPage) {
        model.addAttribute("page", productService.index(null, new Page<Product>(curPage == null ? 1 : curPage, 5)));
        return "backend/product/index";
    }

    @RequestMapping("/create")
    public String create(@ModelAttribute("product") Product product, HttpServletRequest request, Model model, @RequestParam(value = "imgMainfile", required = false) MultipartFile file) {
        if (request.getMethod().equals("POST")) {
            String pathRoot = request.getSession().getServletContext().getRealPath("");
            if (file != null) {
                String imgPath = UploadHelper.uploadImage(pathRoot, file);
                if (imgPath != null) {
                    product.setImgMain(imgPath);
                }
            }
            productService.create(product);
            return "redirect:/backend/product/view?id=" + product.getId();
        }
        model.addAttribute("types", productTypeService.getTypes());
        return "backend/product/create";
    }

    @RequestMapping("/update")
    public String update(@ModelAttribute("product") Product product, Integer id, HttpServletRequest request, Model model, @RequestParam(value = "imgMainfile", required = false) MultipartFile file) {
        model.addAttribute("types", productTypeService.getTypes());
        if (request.getMethod().equals("POST")) {
            String pathRoot = request.getSession().getServletContext().getRealPath("");
            System.out.println(pathRoot);
            if (file != null) {
                String imgPath = UploadHelper.uploadImage(pathRoot, file);
                if (imgPath != null) {
                    product.setImgMain(imgPath);
                }
            }
            productService.update(product);
            return "redirect:/backend/product/view?id=" + product.getId();
        }
        model.addAttribute("product", product.selectOne(new EntityWrapper<Product>().eq("id", id)));
        return "backend/product/update";
    }

    @RequestMapping("/view")
    public String view(Integer id, Model model) {
        model.addAttribute("product", productService.view(id));
        return "backend/product/view";
    }

    @RequestMapping("/delete")
    public String delete(Integer id) {
//        System.out.println(id);
        productService.delete(id);
        return "redirect:/backend/product/index";
    }
}

